{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "[![image](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/opengeos/maxar-open-data/blob/master/examples/morocco_earthquake.ipynb)\n",
    "[![image](https://img.shields.io/badge/Open-Planetary%20Computer-black?style=flat&logo=microsoft)](https://pccompute.westeurope.cloudapp.azure.com/compute/hub/user-redirect/git-pull?repo=https://github.com/opengeos/maxar-open-data&urlpath=lab/tree/maxar-open-data/examples/morocco_earthquake&branch=master)\n",
    "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/maxar-open-data/blob/master/examples/morocco_earthquake.ipynb)\n",
    "[![image](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/opengeos/maxar-open-data/master?urlpath=lab%2Ftree%2Fexamples)\n",
    "\n",
    "**Visualizing Maxar Open Data for the 2023 Morocco Earthquake with Leafmap**\n",
    "\n",
    "The [Maxar Open Data Program](https://www.maxar.com/open-data) provides pre- and post-event high-resolution satellite imagery in support of emergency planning, risk assessment, monitoring of staging areas and emergency response, damage assessment, and recovery. Check out the links below for more information.\n",
    "- [Maxar Open Data Program](https://www.maxar.com/open-data)\n",
    "- [Maxar Open Data on AWS](https://registry.opendata.aws/maxar-open-data/)\n",
    "- [Maxar Open Data on STAC Index](https://stacindex.org/catalogs/maxar-open-data-catalog-ard-format#/)\n",
    "- [Maxar Open Data on STAC Browser](https://radiantearth.github.io/stac-browser/#/external/maxar-opendata.s3.amazonaws.com/events/catalog.json?.language=en)\n",
    "\n",
    "The Maxar Open Data STAC catalog URL is: https://maxar-opendata.s3.amazonaws.com/events/catalog.json\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install -U leafmap geopandas cogeo-mosaic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap.foliumap as leafmap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "Retrieve all collections from the Maxar Open Data STAC catalog. Each collection represents a single event."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "leafmap.maxar_collections()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "Retrieve all collections for a specific event:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "collections = leafmap.maxar_child_collections(\"Morocco-Earthquake-Sept-2023\")\n",
    "print(f\"The number of collections: {len(collections)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "collections.sort()\n",
    "collections"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8",
   "metadata": {},
   "source": [
    "Retrieve all items (tiles) for a specific collection and generate the footprints:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = leafmap.maxar_items(\n",
    "    collection_id=\"Morocco-Earthquake-Sept-2023\",\n",
    "    child_id=\"10300100ECC53700\",\n",
    "    return_gdf=True,\n",
    "    assets=[\"visual\"],\n",
    ")\n",
    "gdf.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "Add the footprints to the map:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_gdf(gdf, layer_name=\"Footprints\", zoom_to_layer=True, info_mode=\"on_click\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12",
   "metadata": {},
   "source": [
    "Retrieve the COG URLs for all tiles in a collection:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "images = gdf[\"visual\"].tolist()\n",
    "images[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14",
   "metadata": {},
   "source": [
    "Download the COGs to a local directory:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "leafmap.maxar_download(images[:2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16",
   "metadata": {},
   "source": [
    "Create a mosaic json file for the collection. You need to install `cogeo-mosaic` first using `pip install cogeo-mosaic`. Creating a mosaic json file might take a few minutes. Please be patient."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "# leafmap.create_mosaicjson(images, output='10300100ECC53700.json')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18",
   "metadata": {},
   "source": [
    "Make the mosaic json file available on the web, then you can add the mosaic to the map:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(height=\"600px\")\n",
    "m.add_basemap(\"SATELLITE\")\n",
    "url = \"https://open.gishub.org/maxar-open-data/datasets/Morocco-Earthquake-Sept-2023/10300100ECC53700.json\"\n",
    "m.add_stac_layer(url, name=\"Mosaic\")\n",
    "m.add_gdf(gdf, layer_name=\"Footprints\", info_mode=\"on_click\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20",
   "metadata": {},
   "source": [
    "Retrieve the footprint of all tiles for a specific event. This might take 15+ minutes. Please be patient."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {},
   "outputs": [],
   "source": [
    "# gdf = leafmap.maxar_all_items(\n",
    "#     collection_id='Morocco-Earthquake-Sept-2023',\n",
    "#     return_gdf=True,\n",
    "#     verbose=True\n",
    "#     )\n",
    "# gdf.to_file('maxar_footprints.geojson', driver='GeoJSON')\n",
    "# gdf"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22",
   "metadata": {},
   "source": [
    "Add the footprints to the map:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=[31.35874, -8.78226], zoom=8)\n",
    "url = \"https://raw.githubusercontent.com/opengeos/maxar-open-data/master/datasets/Morocco-Earthquake-Sept-2023_union.geojson\"\n",
    "m.add_geojson(url, layer_name=\"Footprints\", info_mode=\"on_click\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24",
   "metadata": {},
   "source": [
    "You can find the list of all available images at [Morocco-Earthquake-Sept-2023.tsv](https://github.com/opengeos/maxar-open-data/blob/master/datasets/Morocco-Earthquake-Sept-2023.tsv)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = leafmap.geojson_to_gdf(url)\n",
    "gdf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26",
   "metadata": {},
   "outputs": [],
   "source": [
    "before = gdf[gdf[\"datetime\"] < \"2023-09-10\"]\n",
    "len(before)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27",
   "metadata": {},
   "outputs": [],
   "source": [
    "before.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28",
   "metadata": {},
   "outputs": [],
   "source": [
    "after = gdf[gdf[\"datetime\"] >= \"2023-09-10\"]\n",
    "len(after)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29",
   "metadata": {},
   "outputs": [],
   "source": [
    "after.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_gdf(before, layer_name=\"Before\", info_mode=\"on_click\")\n",
    "style_dict = {\"color\": \"red\", \"fillColor\": \"red\", \"fillOpacity\": 0.1}\n",
    "style_function = lambda x: style_dict\n",
    "m.add_gdf(\n",
    "    after, layer_name=\"After\", info_mode=\"on_click\", style_function=style_function\n",
    ")\n",
    "m"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "vscode": {
   "interpreter": {
    "hash": "31f05ea183a4718249d13ada7f166c6bdba1d00716247af5c11c23af8d5923f1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
